package de.clapp.toolkit;

import java.util.ArrayList;

import android.media.audiofx.BassBoost.Settings;

import de.clapp.backend.network.ClientRequest;
import de.clapp.backend.network.TCPSocket;
import de.clapp.backend.requests.ServerRequest;
import de.clapp.club.ClubListAdapter;
import de.clapp.data.Club;
import de.clapp.data.settings.ServerSettings;

public class ConnectionThread extends Thread
{
    private ServerRequest request;

    private ClubListAdapter adapter;

    private ConnectionHandler handler;

    public ConnectionThread(ServerRequest request, ConnectionHandler handler, ClubListAdapter adapter)
    {
        this.request = request;
        this.adapter = adapter;
        this.handler = handler;
    }

    @Override
    public void run()
    {
        try
        {
            System.out.println("ConnectionThread.run()1");
            ClientRequest r = new ClientRequest(new TCPSocket(de.clapp.Settings.ServerIP, ServerSettings.ServerPort));
            System.out.println("ConnectionThread.run()2");
            r.request(request);
            System.out.println("ConnectionThread.run()3");

            handler.post(new Runnable()
            {
                @SuppressWarnings("unchecked")
                @Override
                public void run()
                {
                    adapter.addAll(((ArrayList<Club>) request.getItem()));
                }
            });

            r.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
